package org.dylan.common.paging.dialect;

import org.springframework.beans.factory.FactoryBean;

public class PageDialectFactoryBean implements FactoryBean<PageDialect> {
	public static enum DBType {
		mysql, oracle, mssql, mssql2000, db2, derby, hsql, postgreSQL, sybase
	}

	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public PageDialect getObject() throws Exception {
		if (this.type == null) {
			return null;
		}
		for (DBType dbType : DBType.values()) {
			if (dbType.name().equalsIgnoreCase(type)) {
				return dialectFromDBType(dbType);
			}
		}
		return null;
	}

	private PageDialect dialectFromDBType(DBType dbType) {
		switch (dbType) {
		case mysql:
			return new MySQLDialect();
		case oracle:
			return new OracleDialect();
		case mssql:
			return new SQLServerDialect();
		case mssql2000:
			return new SQLServerDialect();
		case db2:
			return new DB2Dialect();
		case derby:
			return new DerbyDialect();
		case hsql:
			return new HSQLDialect();
		case postgreSQL:
			return new PostgreSQLDialect();
		case sybase:
			return new SybaseDialect();
		}
		return null;
	}

	@Override
	public Class<?> getObjectType() {
		return PageDialect.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
